/* 
// 在container有滚动时
// 隐藏popper类的组件
scrollHide(document.getElementById('container'),'.popper');
// 隐藏下拉项
scrollHide(document.getElementById('container'),'.el-select-dropdown');
// 隐藏时间弹框
scrollHide(document.getElementById('container'),'.el-date-range-picker');
————————————————
版权声明：本文为CSDN博主「前端的小小对象」的原创文章，遵循CC 4.0 BY-SA版权协议，转载请附上原文出处链接及本声明。
原文链接：https://blog.csdn.net/a736755244/article/details/127683625
 */


let mouseUp = null;
let mouseDown = null;

// 隐藏popper
export const hidePopper = function (cls='.el-popper') {
  let dom = document.querySelector(cls);
  if (!dom) {
    return;
  }

  // 创建鼠标事件
  if (!mouseUp || !mouseDown) {
    mouseUp = new MouseEvent('mouseup', {
      'view': window,
      'bubbles': true,
      'cancelable': true
    });
    mouseDown = new MouseEvent('mousedown', {
      'view': window,
      'bubbles': true,
      'cancelable': true
    });
  }
  // 吮吸触发mouseDown、mouseUp
  dom.dispatchEvent(mouseDown);
  dom.dispatchEvent(mouseUp);
}

// 清除鼠标事件
export const delEvents = function() {
  mouseUp = null;
  mouseDown = null;
}

/**
  * 滚动时隐藏
  * @param contain 滚动监听的容器
  * @param cls 隐藏的元素class:如下拉、时间弹框等
*/
export const scrollHide = function (contain, cls) {
  // console.log('contain=', contain);
  // 先移除
  if (mouseUp || mouseDown) {
    delEvents();
  }
  // 再绑定
  contain.onscroll = function() {
    hidePopper(cls);
  }
}